from django.db import models
from django.contrib import admin

# AP Location to show all the APs in SJTU
class APLocation(models.Model):
	mac_address = models.CharField(max_length = 60)
	ssid = models.CharField(max_length = 60)
	location = models.CharField(max_length = 60)
	field_id = models.IntegerField()
	# outdoor location : unit with GPS unit
	lat = models.FloatField()
	long = models.FloatField()
	# indoor location : unit with mm
	x = models.FloatField() 
	y = models.FloatField()
	class Meta:
		ordering=['mac_address']
	def __unicode__(self):
		return self.mac_address

# The Data to Generate the Local Map 
class BuildingPoint(models.Model):
    field_id = models.IntegerField()
    color = models.IntegerField()
    points = models.TextField()
    # points style: 1,1;1,2;3,5;1,3;4,7;9,0;8,20;1,2;3,4;5,1
    # usage point = points.split(';')
    #       tmp = point.split(',')
    #       using tmp[0], tmp[1] as the line's end location
    # ----------- #
    # points style: 1,1;1,2;3,5|1,3;4,7;9,0;8,20|1,2;3,4;5,1
    # usage shape = points.split('|')
    #       point = shape.split(';')
    #       tmp = point.split(',')
    #       using tmp[0], tmp[1] as the line's end location
    class Meta:
        ordering=['color']
    def __unicode__(self):
        return str(self.color)		

# The information of a user		
#class UserInfo(models.Model):
	
# Record of a user's location indoor
class UserLocation(models.Model):
	user_id = models.IntegerField()
	field_id = models.IntegerField()
	x = models.FloatField()
	y = models.FloatField()
	lat = models.FloatField()
	long = models.FloatField()
	session_id = models.IntegerField()
	count = models.IntegerField()
	timestamp = models.DateTimeField(auto_now=True, auto_now_add=True)

# Record of a user's MacRssi Pair
class UserMacRssi(models.Model):
	user_id = models.IntegerField()
	field_id = models.IntegerField()
	mac_rssi = models.TextField()
	timestamp = models.DateTimeField(auto_now=True, auto_now_add=True)

# The Sample Point location
class SamplePointLoc(models.Model):
	field_id = models.IntegerField()
	x = models.FloatField()
	y = models.FloatField()
	def __unicode__(self):
		return str(self.x) + ',' + str(self.y)
	
class SamplePointRssi(models.Model):
	sample_point = models.ForeignKey(SamplePointLoc)
	mac = models.CharField(max_length = 60)
	rssi = models.IntegerField()
	def __unicode__(self):
		return str(self.sample_point)
	
# Info to record the RSSI of the position		
#class PointMacRssi(models.Model):
#	# the x,y is the local position 
#    x = models.FloatField()
#    y = models.FloatField() 
#    mac_rssi = models.TextField()

### for valid mac address	
#class ValidMac(models.Model):
#	field_id = models.IntegerField()
#	mac = models.CharField(max_length = 60)	
	
class APLocationAdmin(admin.ModelAdmin):
    list_display=('field_id', 'lat', 'long', 'x', 'y', 'mac_address', 'ssid', 'location')

class BuildingPointAdmin(admin.ModelAdmin):
	list_display=('field_id', 'color', 'points')
    
class UserLocationAdmin(admin.ModelAdmin):
	list_display=('user_id', 'field_id', 'lat', 'long', 'x', 'y', 'session_id', 'count', 'timestamp')
	
class UserMacRssiAdmin(admin.ModelAdmin):
	list_display=('user_id', 'field_id', 'mac_rssi', 'timestamp')
	
class SamplePointLocAdmin(admin.ModelAdmin):
	list_display=('field_id', 'x', 'y')
	
class SamplePointRssiAdmin(admin.ModelAdmin):
	list_display=('sample_point', 'mac', 'rssi')
	
admin.site.register(APLocation, APLocationAdmin)
admin.site.register(BuildingPoint, BuildingPointAdmin)
admin.site.register(UserLocation, UserLocationAdmin)
admin.site.register(UserMacRssi, UserMacRssiAdmin)
admin.site.register(SamplePointLoc, SamplePointLocAdmin)
admin.site.register(SamplePointRssi, SamplePointRssiAdmin)